package com.titanrise.crm.mapper;

import com.titanrise.crm.domain.dto.CustomerDto;
import com.titanrise.crm.domain.dto.UserDto;
import com.titanrise.crm.domain.model.User;
import com.titanrise.crm.environment.annotation.*;

import java.util.List;

@Mapper("userMapper")
public interface UserMapper {

    @Select("select * from tb_user")
    @ResultType(User.class)
    List<User> selectAll();

    @Select("SELECT * FROM tb_user WHERE id = #{id}")
    @ResultType(User.class)
    User selectById(@Param("id") String id);

    @Insert(value = "INSERT INTO tb_user VALUES(#{user.id}, #{user.phoneNumber}, " +
            "#{user.annualIncome}, #{user.industry}, #{user.description}, #{user.country}, #{user.region}, " +
            "#{user.province}, #{user.city}, #{user.district}, #{user.postCode}, #{user.createTime})",
            autoGenerateUUID = true)
    int insertOne(@ParamEntity("user") User user);

    @Select("select user.id, user.phone_number, user.annual_income, user.industry, " +
            "rol.role, rol.name, rol.username, rol.password from tb_user user left join" +
            " tb_role rol on user.id = rol.uid")
    @ResultType(UserDto.class)
    List<UserDto> selectAllMemberInfForRoot();

    @Insert("insert into tb_user(id, create_time) value (#{user.id}, #{user.createTime})")
    int addUser(@ParamEntity("user") User user);

    @Delete("delete from tb_user where id = #{uid}")
    int deleteUserByID(@Param("uid") String uid);

    @Select("SELECT tu.*, tr1.name, tr1.email\n" +
            "FROM tb_user AS tu\n" +
            "         LEFT JOIN tb_role AS tr1\n" +
            "                   ON tu.id = tr1.uid\n" +
            "WHERE (SELECT role FROM tb_role AS tr2 WHERE tr2.uid = tu.id) = 0\n" +
            "  AND tu.id IN (SELECT client_id FROM tb_client_sales_relationships AS tcsr WHERE tcsr.sales_id = #{salespersonUid});")
    @ResultType(CustomerDto.class)
    List<CustomerDto> selectBelongedCustomerBySalespersonUid(@Param("salespersonUid") String salespersonId);

    @Select("SELECT tu.*, tr1.name, tr1.email\n" +
            "FROM tb_user AS tu\n" +
            "         LEFT JOIN tb_role AS tr1\n" +
            "                   ON tu.id = tr1.uid\n" +
            "WHERE (SELECT role FROM tb_role AS tr2 WHERE tr2.uid = tu.id) = 0\n" +
            "  AND tu.id NOT IN (SELECT client_id FROM tb_client_sales_relationships);")
    @ResultType(CustomerDto.class)
    List<CustomerDto> selectNonBelongingCustomer();

    @Update("UPDATE tb_user SET phone_number = #{phoneNumber} WHERE id = #{userId}")
    int updatePhoneNumberByUid(@Param("userId") String userId, @Param("phoneNumber") String phoneNumber);

    @Delete("delete from tb_user where id = #{id}")
    int deleteById(@Param("id") String id);

    @Update("update tb_user set phone_number = #{user.phoneNumber}, annual_income = #{user.annualIncome}, " +
            "industry = #{user.industry}, description = #{user.description}, country = #{user.country}, province = #{user.province}, " +
            "city = #{user.city}, district = #{user.district}, post_code = #{user.postCode}")
    int updateOne(@ParamEntity("user") User user);
}
